package cn.learn.jdk8new.stream;

import lombok.var;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author huangyezhan
 * @version 1.0
 * @date 2020年08月12日21:50
 */
public class StreamDemo4 {


    private Book[] books() {
        return new Book[]{
                new Book("CS Book 1", new BigDecimal("9.19"), "CS"),
                new Book("CS Book 2", new BigDecimal("19.23"), "CS"),
                new Book("CS Book 3", new BigDecimal("39.78"), "CS"),

                new Book("Children Book 1", new BigDecimal("3.78"), "Children"),
                new Book("Children Book 2", new BigDecimal("12.78"), "Children"),
                new Book("Children Book 3", new BigDecimal("97.78"), "Children"),

                new Book("Novel Book 1", new BigDecimal("20.78"), "Novel"),
                new Book("Novel Book 1", new BigDecimal("22.58"), "Novel"),
                new Book("Novel Book 1", new BigDecimal("23.80"), "Novel"),
                new Book("Novel Book 1", new BigDecimal("12.74"), "Novel"),
        };
    }

    @Test
    public void test1() throws Exception {
        //用map来做分类的 价格最大的存储
        var map = new HashMap<String, Book>();
        for (Book book : books()) {
            var aBook = map.get(book.getCategory());
            if (aBook != null) {
                if (book.getPrice().compareTo(aBook.getPrice()) > 0) {
                    map.put(book.getCategory(), book);
                }
            } else {
                map.put(book.getCategory(), book);
            }
        }

        List<Book> books = new ArrayList<>(map.values());
        for (Book book : books) {
            System.out.println(book);
        }

        System.out.println("===================================");

        List<Book> collect = Stream.of(books())
                .collect(Collectors.groupingBy(Book::getCategory))
                .entrySet()
                .stream()
                .map(e -> e.getValue()
                        .stream()
                        .sorted(Comparator.comparing(Book::getPrice).reversed())
                        .findFirst()
                        .get())
                .collect(Collectors.toList());

        collect.forEach(System.out::println);
    }


    /**
     * 两个集合匹配
     */
    @Test
    public void test2() throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object>       map1 = new HashMap<>();
        Map<String, Object>       map2 = new HashMap<>();
        Map<String, Object>       map3 = new HashMap<>();

        map1.put("map1", "hao");
        map1.put("id", "1");

        map2.put("map1", "hao11");
        map2.put("id", "2");

        map3.put("map1", "hao12");
        map3.put("id", "3");

        list.add(map1);
        list.add(map2);
        list.add(map3);

        Set<String> collect = list.stream()
                .map(s -> (String) s.get("map1"))
                .collect(Collectors.toSet());
        /*for (String s : collect) {
            System.out.println(s);
        }*/


        List<Map<String, Object>> list2 = new ArrayList<>();
        Map<String, Object>       map4  = new HashMap<>();
        Map<String, Object>       map5  = new HashMap<>();
        Map<String, Object>       map6  = new HashMap<>();
        map4.put("name", "好！");
        map4.put("id", "1");

        map5.put("name", "第二次，是好！");
        map5.put("id", "2");

        map6.put("name", "第三次，真的是好！");
        map6.put("id", "3");

        list2.add(map4);
        list2.add(map5);
        list2.add(map6);

        List<Map<String, Object>> collect1 = list.stream()
                .map(m -> {
                    m.put("name", "");
                    list2.stream()
                            .filter(m2 -> m2.get("id").equals(m.get("id")))
                            .forEach(s -> m.put("name", s.get("name")));
                    return m;
                }).collect(Collectors.toList());

        for (Map<String, Object> map : collect1) {
            System.out.println(map.entrySet());
        }

       /* List<Map<String, Object>> resultList2 = list.stream().map(m -> {
            m.put("name", "");
            list2.stream().filter(m2 -> Objects.equals(m.get("id"), m2.get("id"))).forEach(s -> m.put("name", s.get("name")));
            return m;
        }).collect(Collectors.toList());
        for (Map<String, Object> map : resultList2) {
            System.out.println(map.entrySet());
        }*/


    }


    /**
     * 两个集合匹配
     */
    @Test
    public void test4() throws Exception {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 1; i < 9; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", i);
            map.put("name", "张三丰" + i);
            list.add(map);
        }
        Stream<Map<String, Object>> s1 = list.stream();
        list.stream().forEach(map -> System.out.println(map));

        List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
        for (int i = 1; i < 5; i++) {
            Map<String, Object> map2 = new HashMap<>();
            map2.put("id", i);
            map2.put("grade", i + 60);
            list2.add(map2);
        }
        list2.stream().forEach(s -> System.out.println(s));
/**
 * List<Map<Object, Object>> resultList = oneList.stream().map(map -> twoList.stream()
 *         .filter(m -> Objects.equals(m.get("id"), map.get("id")))
 *         .findFirst().map(m -> {
 *           map.putAll(m);
 *           map.put("grade",90);
 *           return map;
 *         }).orElse(null))
 *         .filter(Objects::nonNull).collect(Collectors.toList());
 */
   /* List<Map<String, Object>> resultList2 = list.stream().map(m->{
          m.put("grade",0);
          for (int i=0;i<list2.size();i++){
            if(m.get("id").equals(list2.get(i).get("id"))){
              m.put("grade",list2.get(i).get("grade"));
              break;
            }
          }
          return m;
        }).collect(Collectors.toList());*/


        List<Map<String, Object>> resultList2 = list.stream()
                .map(m -> {
                    m.put("grade", 0);
                    list2.stream()
                            .filter(m2 -> Objects.equals(m.get("id"), m2.get("id")))
                            .forEach(s -> m.put("grade", s.get("grade")));
                    return m;
                }).collect(Collectors.toList());
        resultList2.stream().forEach(s -> System.out.println(s));

        System.out.println("================================");

        List<Map<String, Object>> list3 = list.stream()
                .map(map -> list2.stream()
                        .filter(m -> Objects.equals(m.get("id"), map.get("id")))
                        .findFirst()
                        .map(m -> {
                            map.putAll(m);
                            return map;
                        }).orElse(null))
                .filter(Objects::nonNull).collect(Collectors.toList());

        for (Map<String, Object> map : list3) {
            System.out.println(map.entrySet());
        }
    }


}
